﻿using TjZheng.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Controls;
using TjZheng.Domain.Business;
using TjZheng.Domain.DataEntity;
using TjZheng.CMS.Process;

namespace TjZheng.CMS.Controllers
{
    public class UploadLimitController : CMSController
    {
        BizUploadLimit objBizUploadLimit { get; set; }
        public UploadLimitController()
        {
            objBizUploadLimit = new BizUploadLimit();
        }

        public ActionResult Index()
        {
            return View();
        }

        [QueryMsSqlFilter("UcId")]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            return Json(objBizUploadLimit.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "Id"));
        }

        public ActionResult EditPage(long? id)
        {
            if (TransferID.IsNull())
            {
                return Content("资源类型Id不存在！");
            }
            BizUploadCategory objBizUploadCategory = new BizUploadCategory();
            var objUploadCategory = objBizUploadCategory.GetRecord(TransferID.ConvertLong());
            if (objUploadCategory == null)
            {
                return Content("未找到资源类型！");
            }
            UploadLimit objUploadLimit = null;
            if (id.HasValue)
            {
                objUploadLimit = objBizUploadLimit.GetRecord(id.Value);
            }
            else
            {
                objUploadLimit = new UploadLimit { UcId = objUploadCategory.Id, UcCode = objUploadCategory.CategoryCode };
            }
            return View(objUploadLimit);
        }

        //[ValidateAntiForgeryToken]
        public ActionResult Save(UploadLimit objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            UploadLimit objTarget = null;
            if (objProvide.Id <= 0)
            {
                objTarget = new UploadLimit();
                Save(objTarget, objProvide);
                objBizUploadLimit.Add(objTarget);
            }
            else
            {
                objTarget = objBizUploadLimit.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizUploadLimit.Update(objTarget);
            }
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }

        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizUploadLimit.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }

        void Save(UploadLimit objTarget, UploadLimit objProvide)
        {
            objTarget.Name = objProvide.Name.CutWord(64);
            objTarget.LimitCode = objProvide.LimitCode.CutWord(64);
            objTarget.UcId = objProvide.UcId;
            objTarget.UcCode = objProvide.UcCode.CutWord(64);
            objTarget.MediaType = objProvide.MediaType;
            objTarget.SaveType = objProvide.SaveType;
            objTarget.UserName = objProvide.UserName.CutWord(64);
            objTarget.Password = objProvide.Password.CutWord(64);
            objTarget.ServerIP = objProvide.ServerIP.CutWord(256);
            objTarget.ServerPath = objProvide.ServerPath.CutWord(256);
            objTarget.LimitExtension = objProvide.LimitExtension.CutWord(256);
            objTarget.LimitSize = objProvide.LimitSize;
        }
    }
}
